﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using CE.CarInsurance;

public partial class Admin_car_insurance_AddNewCarMake : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/Admin/AdminSecureLogin.aspx");
        }

        if (!Page.IsPostBack)
        {
            if (Request.Params["CarMakeId"] != null)
            {
                int _carmakeid;
                if (Int32.TryParse(Request.Params["CarMakeId"].ToString(), out _carmakeid))
                {
                    ShowCarMake(_carmakeid);
                }
            }
        }
    }

    private void ShowCarMake(int CarMakeId)
    {
        CarMake _carmake = new CarMake();
        CarMakeDB _carmakedb = new CarMakeDB();
        _carmake = _carmakedb.GetCarMake(CarMakeId);
        if (_carmake != null)
        {
            lblCarMakeId.Text = _carmake.CarMakeAutoId.ToString();
            txtCarMakeId.Text = _carmake.CarMakeId.ToString();
            //txtCarMakeId.Enabled = false;
            txtDescription.Text = _carmake.Description;
            chkCarMakeStatus.Checked = _carmake.Status;
            btnSubmit.Text = "Update";
        }
    }

    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        ClearForm();
    }

    private void ClearForm()
    {
        lblMessage.Text = "";
        lblCarMakeId.Text = "";
        txtCarMakeId.Text = "";
        txtDescription.Text = "";
        chkCarMakeStatus.Checked = false;
        //txtCarMakeId.Enabled = true;
        btnSubmit.Text = "Submit";
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        CarMake _carmake = new CarMake();
        CarMakeDB _carmakedb = new CarMakeDB();

        int _carmakeId;
        int _carmakeautoid;
        txtDescription.Text = txtDescription.Text.Trim();

        bool _isexist = false;

        if (lblCarMakeId.Text.Trim().Length == 0)
        {
            _carmakeId = _carmakedb.GetCarMakeNextId(txtDescription.Text.Trim());

            if (_carmakeId > 0)
            {
                txtCarMakeId.Text = _carmakeId.ToString();
            }
            else
            {
                lblMessage.Text = "Car Make ID is invalid or zero!";
                return;
            }
        }
        else
        {
            if (Int32.TryParse(txtCarMakeId.Text, out _carmakeId) == false)
            {
                lblMessage.Text = "Can't update!";
                return;
            }
        }

        if (!Page.IsValid)
        {
            lblMessage.Text = "Page is not valid!";
            return;
        }

        if (_carmakeId != 0)
        {
            _isexist = _carmakedb.CheckCarMakeId(_carmakeId, out _carmakeautoid);

            if (_isexist)
            {
                if (lblCarMakeId.Text.Trim().Length == 0)
                {
                    lblMessage.Text = "Car Make Id already exists!";
                    txtCarMakeId.Focus();
                    return;
                }
                else
                {
                    _carmake.CarMakeAutoId = _carmakeautoid;
                }
            }

            _carmake.CarMakeId = _carmakeId;
            _carmake.Description = txtDescription.Text;
            _carmake.Status = chkCarMakeStatus.Checked;

            _carmake.CarMakeAutoId = _carmakedb.Insert(_carmake);
            if (_carmake.CarMakeAutoId > 0)
            {
                if (btnSubmit.Text.ToUpper() == "UPDATE")
                {
                    lblMessage.Text = "Update successfully!";
                }
                else
                {
                    lblMessage.Text = "Insert successfully!";
                }
            }
            else
            {
                if (btnSubmit.Text.ToUpper() == "UPDATE")
                {
                    lblMessage.Text = "Update failed!";
                }
                else
                {
                    lblMessage.Text = "Insert failed!";
                }
            }

        }
    }
}
